package com.zklh.leetcode.other;

/**
 * 检查骑士巡视方案
 *
 * @version 1.0
 * @Author tongnan
 * @Date 2023/9/18
 */
public class Solution2596 {
    public boolean checkValidGrid(int[][] grid) {
        if (grid[0][0] != 0) {
            return false;
        }
        int rowSize = grid.length;
        int colSize = grid[0].length;
        int[][] trace = new int[rowSize * colSize][2];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                trace[grid[i][j]] = new int[]{i, j};
            }
        }

        for (int i = 0; i < trace.length - 1; i++) {
            if (Math.abs(trace[i + 1][0] - trace[i][0]) * Math.abs(trace[i + 1][1] - trace[i][1]) != 2 ) {
                return false;
            }
        }

        return true;
    }

}